import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let username = localStorage.getItem('username') || '未登录'
let userStatus = localStorage.getItem('userStatus') || false
let nickname = localStorage.getItem('nickname') || '未登录'
let avatar = localStorage.getItem('avatar') || 'static/avatar.jpeg'

// let username = '未登录'
// let userStatus = false
// let nickname = '未登录'
// let avatar = 'static/avatar.jpeg'

export default new Vuex.Store({
  state: {
    username,
    userStatus,
    nickname,
    avatar
  },
  mutations: {
    changeUser (state, user) {
      state.username = user.username
      state.userStatus = user.userStatus
      state.nickname = user.nickname
      state.avatar = user.avatar
      localStorage.setItem('username', user.username)
      localStorage.setItem('userStatus', user.userStatus)
      localStorage.setItem('nickname', user.nickname)
      localStorage.setItem('avatar', user.avatar)
      if (!user.userStatus) {
        localStorage.removeItem('username')
        localStorage.removeItem('userStatus')
        localStorage.removeItem('nickname')
        localStorage.removeItem('avatar')
      }
    },
    changeNickname (state, user) {
      state.nickname = user.nickname
      localStorage.setItem('nickname', user.nickname)
    },
    changeAvatar (state, user) {
      state.avatar = user.avatar
      localStorage.setItem('avatar', user.avatar)
    }
  }
})
